package com.huawei.hianalytics.process;

import android.content.Context;
import com.huawei.hianalytics.g.b;
import com.huawei.hianalytics.util.g;

public final class HiAnalyticsInstance$Builder {
    private HiAnalyticsConfig diffConf = null;
    private Context mContext;
    private HiAnalyticsConfig maintConf = null;
    private HiAnalyticsConfig operConf = null;

    public HiAnalyticsInstance$Builder(Context context) {
        if (context != null) {
            this.mContext = context.getApplicationContext();
        }
    }

    private void setConf(d dVar) {
        if (this.operConf == null) {
            dVar.c(null);
        } else {
            dVar.c(new HiAnalyticsConfig(this.operConf));
        }
        if (this.maintConf == null) {
            dVar.a(null);
        } else {
            dVar.a(new HiAnalyticsConfig(this.maintConf));
        }
        if (this.diffConf == null) {
            dVar.b(null);
        } else {
            dVar.b(new HiAnalyticsConfig(this.diffConf));
        }
    }

    public HiAnalyticsInstance create(String str) {
        if (this.mContext == null) {
            b.d("HianalyticsSDK", "create(): instance context is null,create failed!");
            return null;
        } else if (str == null || !g.a("tag", str, "[a-zA-Z0-9][a-zA-Z0-9_]{0,255}")) {
            b.d("HianalyticsSDK", "create(): check tag failed! TAG: " + str);
            return null;
        } else if (HiAnalyticsManager.getInitFlag(str)) {
            b.d("HianalyticsSDK", "This tag already exists");
            return null;
        } else if (a.b().c(str)) {
            b.d("HianalyticsSDK", "create(): black tag is not allowed here.");
            return null;
        } else if (a.b().a() - a.b().e() > 50) {
            b.d("HianalyticsSDK", "The number of TAGs exceeds the limit!");
            return null;
        } else {
            HiAnalyticsInstance dVar = new d(str);
            setConf(dVar);
            a.b().a(this.mContext);
            b.a().a(this.mContext, str);
            d a = a.b().a(str, dVar);
            return a != null ? a : dVar;
        }
    }

    public HiAnalyticsInstance refresh(String str) {
        HiAnalyticsInstance a = a.b().a(str);
        if (a == null) {
            b.c("HianalyticsSDK", "HiAnalyticsInstance.Builder.Refresh(): calling refresh before create. TAG: " + str + " has no instance. ");
            return create(str);
        }
        a.refresh(1, this.maintConf);
        a.refresh(0, this.operConf);
        a.refresh(3, this.diffConf);
        return a;
    }

    public HiAnalyticsInstance$Builder setDiffConf(HiAnalyticsConfig hiAnalyticsConfig) {
        this.diffConf = hiAnalyticsConfig;
        return this;
    }

    public HiAnalyticsInstance$Builder setMaintConf(HiAnalyticsConfig hiAnalyticsConfig) {
        this.maintConf = hiAnalyticsConfig;
        return this;
    }

    public HiAnalyticsInstance$Builder setOperConf(HiAnalyticsConfig hiAnalyticsConfig) {
        this.operConf = hiAnalyticsConfig;
        return this;
    }
}
